package com.hengxinguotong.zhihuichengjian.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.MyFragmentPagerAdapter;
import com.hengxinguotong.zhihuichengjian.fragment.GuidePageFragment;
import com.hengxinguotong.zhihuichengjian.widget.view.StatusBarCompat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/19.
 */

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    @BindView(R.id.guide_vp)
    ViewPager guideVp;
    @BindView(R.id.point_container)
    LinearLayout pointContainer;

    private GuidePageFragment guideFragment0, guideFragment1;
    private List<Fragment> fragments;
    private int currentPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guidepage);
        ButterKnife.bind(this);
        initViewPager();
    }

    private void initViewPager(){
        fragments = new ArrayList<Fragment>();
        if(guideFragment0 == null){
            guideFragment0 = GuidePageFragment.newInstance(0);
            fragments.add(guideFragment0);
        }
        if(guideFragment1 == null){
            guideFragment1 = GuidePageFragment.newInstance(1);
            fragments.add(guideFragment1);
        }
        guideVp.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        guideVp.addOnPageChangeListener(this);
        initPoint();
    }

    private void initPoint(){
        for(int i=0; i<fragments.size(); i++){
            ImageView dot = new ImageView(this);
            if(i == currentPage){
                dot.setImageResource(R.mipmap.lanse);
            }else{
                dot.setImageResource(R.mipmap.huise);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout
                    .LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                params.leftMargin = 10;//设置圆点边距
            }
            dot.setLayoutParams(params);
            guideVp.setCurrentItem(0);
            pointContainer.addView(dot);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
        for (int i = 0; i < pointContainer.getChildCount(); i++) {
            ImageView imageView = (ImageView) pointContainer.getChildAt(i);
            if (i == position) {
                imageView.setImageResource(R.mipmap.lanse);
            } else {
                imageView.setImageResource(R.mipmap.huise);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
